package com.bridgeintelligent.tag.webserver.dim.mgmt.service.assess;

import com.bridgeintelligent.tag.mapper.AssessMapper;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.SecurityHelper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.BaseCode;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimPage;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimRep;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.entity.AssessInfo;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.vo.AssessCombinationVO;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.vo.AssessCustomersVO;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.vo.DimAssessVO;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.assess.model.AssessModel;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.assess.model.CombinationModel;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.assess.model.CustomersModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wayneleo.quickstart.framework.IDGenerator;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description
 * @author: grp 2020-09-03 15:21
 **/
@Service
@AllArgsConstructor
@Slf4j
public class AssessServiceImpl implements IAssessService {
    
    private AssessMapper assessMapper;
    
    @Override
    public DimAssessVO getInfo(String dimId) {
        DimAssessVO assessVO = new DimAssessVO();
        DimAssessVO.DimBaseInfo dimBaseInfo = assessMapper.findDimBaseInfo(dimId);
        getConvertDate(dimBaseInfo);
        assessVO.setBaseInfo(dimBaseInfo);
        List<DimAssessVO.DimQualityInfo> dimQualityInfo;
        if(StringUtils.equals("02",dimBaseInfo.getDimLargeType())){
            dimQualityInfo = assessMapper.findDimFreeQualityInfo(dimId);
        }else{
            dimQualityInfo = assessMapper.findDimQualityInfo(dimId);
        }
        if(!CollectionUtils.isEmpty(dimQualityInfo)){
            assessVO.setQualityInfo(dimQualityInfo.get(0));
        }
        List<DimAssessVO.DimUseInfo> dimUseInfo = assessMapper.findDimUseInfo(dimId);
        assessVO.setUseInfo(dimUseInfo);
        User user = SecurityHelper.currentUser();
        assessVO.setAssessInfo(assessMapper.selectByOwner(user.getUserId(),dimId));
        return assessVO;
    }

    private void getConvertDate(DimAssessVO.DimBaseInfo dimBaseInfo) {
        if(dimBaseInfo==null){
            return;
        }
        if(StringUtils.length(dimBaseInfo.getUptTime())>10){
            dimBaseInfo.setUptTime(StringUtils.substring(dimBaseInfo.getUptTime(),0,10));
        }
        if(StringUtils.equals("02",dimBaseInfo.getDimLargeType())||StringUtils.equals("04",dimBaseInfo.getDimLargeType())){
            dimBaseInfo.setEffectiveTime(dimBaseInfo.getApproveDate());
        }
        if(StringUtils.length(dimBaseInfo.getEffectiveTime())>10){
            dimBaseInfo.setEffectiveTime(StringUtils.substring(dimBaseInfo.getEffectiveTime(),0,10));
        }
    }

    @Override
    public void assess(AssessModel assessModel) {
        String id = assessModel.getId();
        User user = SecurityHelper.currentUser();
        AssessInfo info = new AssessInfo();
        BeanUtils.copyProperties(assessModel,info);
        info.setOwner(user.getUserId());
        if(StringUtils.isBlank(id)){
            info.setId(IDGenerator.gen());
            info.setCreateTime(new Date());
            info.setUpdateTime(info.getCreateTime());
            assessMapper.saveAssessInfo(info);
        }else{
            info.setUpdateTime(new Date());
            assessMapper.updateAssessInfo(info);
        }
    }

    @Override
    public DimRep getUsedDims(CombinationModel model) {
        Page page = PageHelper.startPage(model.getPageNumber(), model.getPageSize());
        DimPage<AssessCombinationVO> dimPage = new DimPage<>();
        dimPage.setContent(assessMapper.selectUsedDims(model.getId()));
        dimPage.setTotalPages(page.getPages());
        dimPage.setTotalElements(page.getTotal());
        dimPage.setSize(page.getPageSize());
        dimPage.setRequestPager(model.getPageNumber());
        return new DimRep<>(dimPage, BaseCode.SUCCESS);
    }

    @Override
    public DimRep getUsedCustomers(CustomersModel model) {
        Page page = PageHelper.startPage(model.getPageNumber(), model.getPageSize());
        DimPage<AssessCustomersVO> dimPage = new DimPage<>();
        dimPage.setContent(assessMapper.selectUsedCustomers(model.getId()));
        dimPage.setTotalPages(page.getPages());
        dimPage.setTotalElements(page.getTotal());
        dimPage.setSize(page.getPageSize());
        dimPage.setRequestPager(model.getPageNumber());
        return new DimRep<>(dimPage, BaseCode.SUCCESS);
    }
}
